{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Two Out of Three"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: twoOutOfThree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #至少在两个数组中出现的值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "给你三个整数数组 <code>nums1</code>、<code>nums2</code> 和 <code>nums3</code> ，请你构造并返回一个 <strong>元素各不相同的</strong> 数组，且由 <strong>至少</strong> 在 <strong>两个</strong> 数组中出现的所有值组成<em>。</em>数组中的元素可以按 <strong>任意</strong> 顺序排列。\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]\n",
    "<strong>输出：</strong>[3,2]\n",
    "<strong>解释：</strong>至少在两个数组中出现的所有值为：\n",
    "- 3 ，在全部三个数组中都出现过。\n",
    "- 2 ，在数组 nums1 和 nums2 中出现过。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]\n",
    "<strong>输出：</strong>[2,3,1]\n",
    "<strong>解释：</strong>至少在两个数组中出现的所有值为：\n",
    "- 2 ，在数组 nums2 和 nums3 中出现过。\n",
    "- 3 ，在数组 nums1 和 nums2 中出现过。\n",
    "- 1 ，在数组 nums1 和 nums3 中出现过。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]\n",
    "<strong>输出：</strong>[]\n",
    "<strong>解释：</strong>不存在至少在两个数组中出现的值。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums1.length, nums2.length, nums3.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums1[i], nums2[j], nums3[k] &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [two-out-of-three](https://leetcode.cn/problems/two-out-of-three/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [two-out-of-three](https://leetcode.cn/problems/two-out-of-three/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,1,3,2]\\n[2,3]\\n[3]', '[3,1]\\n[2,3]\\n[1,2]', '[1,2,2]\\n[4,3,3]\\n[5]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1=set(nums1)\n",
    "        nums2=set(nums2)\n",
    "        nums3=set(nums3)\n",
    "        nums={i:0 for i in nums1}\n",
    "        for i in nums2:\n",
    "            if i in nums:\n",
    "                nums[i]=True\n",
    "            else:\n",
    "                nums[i]=False\n",
    "        for i in nums3:\n",
    "            if i in nums:\n",
    "                nums[i]=True\n",
    "            else:\n",
    "                nums[i]=False\n",
    "        a=[]\n",
    "        for i,t in nums.items():\n",
    "            if t:\n",
    "                a.append(i)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        a={}\n",
    "        p=0\n",
    "        for j in (nums1,nums2,nums3):\n",
    "            for i in j:\n",
    "                if i in a.keys():\n",
    "                    a[i].add(p)\n",
    "                else:\n",
    "                    a[i]=set()\n",
    "                    a[i].add(p)\n",
    "            p+=1\n",
    "        b=[]\n",
    "        for i in a.keys():\n",
    "            if len(a[i])>1:\n",
    "                b.append(i)\n",
    "        return b\n",
    "            \n",
    "        \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        list_tem = []\n",
    "        for i in nums1+nums2+nums3:\n",
    "            if (i in nums1 and i in nums2) or (i in nums2 and i in nums3) or (i in nums1 and i in nums3):\n",
    "                list_tem.append(i)\n",
    "        return list(set(list_tem))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        set1, set2, set3 = set(nums1), set(nums2), set(nums3)\n",
    "        result = set()\n",
    "\n",
    "        for num in set1:\n",
    "            if num in set2 or num in set3:\n",
    "                result.add(num)\n",
    "\n",
    "        for num in set2:\n",
    "            if num in set1 or num in set3:\n",
    "                result.add(num)\n",
    "\n",
    "        for num in set3:\n",
    "            if num in set1 or num in set2:\n",
    "                result.add(num)\n",
    "\n",
    "        return list(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        return list((set(nums1)&set(nums2))|(set(nums1)&set(nums3))|(set(nums2)&set(nums3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        cnt=Counter(list(set(nums1))+list(set(nums2))+list(set(nums3)))\n",
    "        return list(set(k for k,v in cnt.items() if v>=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1 = list(set(nums1))\n",
    "        nums2 = list(set(nums2))\n",
    "        nums3 = list(set(nums3))\n",
    "        totalList = nums1+nums2+nums3\n",
    "        tmpDict = {}\n",
    "        for line in set(totalList):\n",
    "            tmpDict[str(line)] = 0\n",
    "        for line in totalList:\n",
    "            tmpDict[str(line)] += 1\n",
    "        dataList = []\n",
    "        for line in tmpDict:\n",
    "            if tmpDict[line] >= 2:\n",
    "                dataList.append(int(line))\n",
    "        return dataList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1 = list(set(nums1))\n",
    "        nums2 = list(set(nums2))\n",
    "        nums3 = list(set(nums3))\n",
    "        nums = nums1 + nums2 + nums3\n",
    "        ans = []\n",
    "        for i in nums:\n",
    "            if nums.count(i) >= 2:\n",
    "                ans.append(i)\n",
    "        ans = list(set(ans))\n",
    "        return [i for i in ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        res=[]\n",
    "        for i in nums1:\n",
    "            if i in nums2 or i in nums3:\n",
    "                res.append(i)\n",
    "        for i in nums2:\n",
    "            if i in nums1 or i in nums3:\n",
    "                res.append(i)                \n",
    "        for i in nums3:\n",
    "            if i in nums1 or i in nums2:\n",
    "                res.append(i)                \n",
    "        res=set(res)\n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        mask = defaultdict(int)\n",
    "        for i, nums in enumerate((nums1, nums2, nums3)):\n",
    "            for x in nums:\n",
    "                mask[x] |= 1 << i\n",
    "        return [x for x, m in mask.items() if m & (m - 1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        set1 = set(nums1) \n",
    "        set2 = set(nums2)\n",
    "        set3 = set(nums3) \n",
    "        return list(set1.intersection(set2).union(set1.intersection(set3).union(set2.intersection(set3))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        s1, s2, s3 = set(nums1), set(nums2), set(nums3)\n",
    "        list1 = list(s1) + list(s2) + list(s3)\n",
    "        set1 = set(list1)\n",
    "        ans = []\n",
    "        for i in set1:\n",
    "            if list1.count(i) >= 2:\n",
    "                ans.append(i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums = list(set(nums1)) + list(set(nums2)) + list(set(nums3))\n",
    "        counter = Counter(nums)\n",
    "        return [k for k,v in counter.items() if v >= 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        q,e,f=Counter(nums1),Counter(nums2),Counter(nums3)\n",
    "        r=q&e\n",
    "        l=e&f\n",
    "        c=f&q\n",
    "        v=Counter()\n",
    "        ans=[]\n",
    "        for x in r.items():\n",
    "           if x[0] not in v:\n",
    "             v[x[0]]+=1\n",
    "             ans.append(x[0])\n",
    "        for x in l.items():\n",
    "           if x[0] not in v:\n",
    "             v[x[0]]+=1\n",
    "             ans.append(x[0])\n",
    "        for x in  c.items():\n",
    "           if  x[0] not in v:\n",
    "             v[x[0]]+=1\n",
    "             ans.append(x[0]) \n",
    "        return ans    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        mask = defaultdict(int)\n",
    "        for i,nums in enumerate((nums1,nums2,nums3)):\n",
    "            for x in nums:\n",
    "                mask[x] |= 1<<i\n",
    "        return [x for x,m in mask.items() if m&(m-1)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1 = list(set(nums1))\n",
    "        nums2 = list(set(nums2))\n",
    "        nums3 = list(set(nums3))\n",
    "        nums = nums1 + nums2 + nums3\n",
    "        ans = []\n",
    "        for i in nums:\n",
    "            if nums.count(i) >= 2 and i not in ans:\n",
    "                ans.append(i)\n",
    "\n",
    "        return [i for i in ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        merged_nums = nums1 + nums2 + nums3\n",
    "        unique_nums = sorted(list(set(merged_nums)))\n",
    "        res=[]\n",
    "        for i in unique_nums:\n",
    "            if (i in nums1 and i in nums2) or (i in nums1 and i in nums3) or (i in nums2 and i in nums3):\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        s1=set(nums1)\n",
    "        s2=set(nums2)\n",
    "        s3=set(nums3)\n",
    "        return [i for i in range(1,101) if (i in s1)+(i in s2)+(i in s3)>1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        s1, s2, s3 = set(nums1), set(nums2), set(nums3)\n",
    "        return [i for i in range(1, 101) if (i in s1) + (i in s2) + (i in s3) > 1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        set1 = set(nums1)\n",
    "        set2 = set(nums2)\n",
    "        set3 = set(nums3)\n",
    "        return [i for i in range(1,101) if (i in set1)+(i in set2)+(i in set3)>1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        lst = [0] * 101\n",
    "        for n in nums1:\n",
    "            lst[n] = 1\n",
    "        for n in set(nums2):\n",
    "            lst[n] += 1\n",
    "        for n in set(nums3):\n",
    "            lst[n] += 1\n",
    "        print(lst)\n",
    "        ans = []\n",
    "        for i, n in enumerate(lst):\n",
    "            if n >= 2:\n",
    "                ans.append(i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1=set(nums1)\n",
    "        nums2=set(nums2)\n",
    "        nums3=set(nums3)\n",
    "        nums={i:0 for i in nums1}\n",
    "        for i in nums2:\n",
    "            if i in nums:\n",
    "                nums[i]=True\n",
    "            else:\n",
    "                nums[i]=False\n",
    "        for i in nums3:\n",
    "            if i in nums:\n",
    "                nums[i]=True\n",
    "            else:\n",
    "                nums[i]=False\n",
    "        a=[]\n",
    "        for i,t in nums.items():\n",
    "            if t:\n",
    "                a.append(i)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        mask = defaultdict(int)\n",
    "        for i, nums in enumerate((nums1, nums2, nums3)):\n",
    "            for x in nums:\n",
    "                mask[x] |= 1 << i\n",
    "        return [x for x, m in mask.items() if m & (m - 1)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums = list(set(nums1)) + list(set(nums2)) + list(set(nums3))\n",
    "        counter = Counter(nums)\n",
    "        return [k for k,v in counter.items() if v >= 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums = list(set(nums1)) + list(set(nums2)) + list(set(nums3))\n",
    "        counter = Counter(nums)\n",
    "        return [k for k,v in counter.items() if v >= 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        set1, set2, set3 = set(nums1), set(nums2), set(nums3)\n",
    "        common_elements = (set1 & set2) | (set1 & set3) | (set2 & set3)\n",
    "        return list(common_elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        cnt = defaultdict(int)\n",
    "        for i, nums in enumerate((nums1, nums2, nums3)):\n",
    "            for x in nums:\n",
    "                cnt[x] |= 1 << i\n",
    "        return [k for k, v in cnt.items() if v & (v-1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        lst=[]\n",
    "        for i in nums1:\n",
    "            if i in nums2 or i in nums3:\n",
    "                lst.append(i)\n",
    "        for i in nums2:\n",
    "            if i in nums1 or i in nums3:\n",
    "                lst.append(i)\n",
    "        for i in nums3:\n",
    "            if i in nums1 or i in nums2:\n",
    "                lst.append(i)\n",
    "        l1 = set(lst)\n",
    "        l2 = list(l1)\n",
    "        return l2\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        lst=[]\n",
    "        for i in range(1,101):\n",
    "            if (i in nums1) + (i in nums2) + (i in nums3) >= 2:\n",
    "                lst.append(i)\n",
    "        return lst\n",
    "        # for i in nums1:\n",
    "        #     if i in nums2 or i in nums3:\n",
    "        #         lst.append(i)\n",
    "        # for i in nums2:\n",
    "        #     if i in nums1 or i in nums3:\n",
    "        #         lst.append(i)\n",
    "        # for i in nums3:\n",
    "        #     if i in nums1 or i in nums2:\n",
    "        #         lst.append(i)\n",
    "        # l1 = set(lst)\n",
    "        # l2 = list(l1)\n",
    "        # return l2\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(\n",
    "        self, \n",
    "        nums1: List[int], \n",
    "        nums2: List[int], \n",
    "        nums3: List[int]\n",
    "    ) -> List[int]:\n",
    "\n",
    "        return list(\n",
    "            (set(nums1) & set(nums2)) | \n",
    "            (set(nums1) & set(nums3)) | \n",
    "            (set(nums2) & set(nums3))\n",
    "        )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        s1, s2, s3 = set(nums1), set(nums2), set(nums3)\n",
    "        return list(s1 & s2 | s2 & s3 | s1 & s3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums = list(set(nums1)) + list(set(nums2)) + list(set(nums3))\n",
    "        counter = Counter(nums)\n",
    "        return [k for k,v in counter.items() if v >= 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: list[int], nums2: list[int], nums3: list[int]) -> list[int]:\n",
    "        nums_set=set(nums1)|set(nums2)|set(nums3)\n",
    "        lst=[]\n",
    "        dic={}\n",
    "        print(nums_set)\n",
    "        for item in nums_set:\n",
    "            dic[item]=0\n",
    "            if item in nums1:\n",
    "                dic[item]+=1\n",
    "            if item in nums2:\n",
    "                dic[item]+=1\n",
    "            if item in nums3:\n",
    "                dic[item]+=1\n",
    "        print(dic)\n",
    "        for key,value in dic.items():\n",
    "            if value>1:\n",
    "                lst.append(key)\n",
    "        return lst\n",
    "\n",
    "a=Solution()\n",
    "print(a.twoOutOfThree([1,1,3,2],[2,3],[3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        a = set(nums1)\n",
    "        b = set(nums2)\n",
    "        c = set(nums3)\n",
    "        return list((a & b) | (b & c) | (a & c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        # 找出来至少在 2 个数组中出现的数，\n",
    "        l1=set(nums1)\n",
    "        l2=set(nums2)\n",
    "        l3=set(nums3)\n",
    "        ans=list(l1&l2| l2&l3 | l1&l3)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        ans.extend(list(set(nums1) & set(nums2)))\n",
    "        ans.extend(list(set(nums1) & set(nums3)))\n",
    "        ans.extend(list(set(nums3) & set(nums2)))\n",
    "        return list(set(ans))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        cnt = []\n",
    "        nums1Set = set(nums1)\n",
    "        nums2Set = set(nums2)\n",
    "        nums3Set = set(nums3)\n",
    "        cnt.extend(list(nums1Set.intersection(nums2Set)))\n",
    "        cnt.extend(list(nums1Set.intersection(nums3Set)))\n",
    "        cnt.extend(list(nums2Set.intersection(nums3Set)))\n",
    "        return list(set(cnt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1=set(nums1)\n",
    "        nums2=set(nums2)\n",
    "        nums3=set(nums3)\n",
    "        nums={i:0 for i in nums1}\n",
    "        for i in nums2:\n",
    "            if i in nums:\n",
    "                nums[i]=True\n",
    "            else:\n",
    "                nums[i]=False\n",
    "        for i in nums3:\n",
    "            if i in nums:\n",
    "                nums[i]=True\n",
    "            else:\n",
    "                nums[i]=False\n",
    "        a=[]\n",
    "        for i,t in nums.items():\n",
    "            if t:\n",
    "                a.append(i)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        ls = set(nums1 + nums2 + nums3)\n",
    "        dic = {'nums1':set(nums1), 'nums2':set(nums2), 'nums3':set(nums3)}\n",
    "        res = []\n",
    "        for i in ls:\n",
    "            tmp = 0\n",
    "            for k in dic:\n",
    "                if i in dic[k]:\n",
    "                    tmp += 1\n",
    "            if tmp > 1:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums = list(set(nums1)) + list(set(nums2)) + list(set(nums3))\n",
    "        counter = Counter(nums)\n",
    "        return [k for k,v in counter.items() if v >= 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        a1=set(nums1)\n",
    "        a2=set(nums2)\n",
    "        a3=set(nums3)\n",
    "        res=[]\n",
    "        for i in range(1,101):\n",
    "            if (i in a1)+(i in a2)+(i in a3)>1:\n",
    "               res.append(i)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(\n",
    "        self, \n",
    "        nums1: List[int], \n",
    "        nums2: List[int], \n",
    "        nums3: List[int]\n",
    "    ) -> List[int]:\n",
    "\n",
    "        num2set = defaultdict(set)\n",
    "\n",
    "        for num in nums1:\n",
    "            num2set[num].add(1)\n",
    "        for num in nums2:\n",
    "            num2set[num].add(2)\n",
    "        for num in nums3:\n",
    "            num2set[num].add(3)\n",
    "\n",
    "        res = []\n",
    "        for num, s in num2set.items():\n",
    "            if len(s) >= 2:\n",
    "                res.append(num)\n",
    "        \n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        res = set()\n",
    "        for i in nums1:\n",
    "            if i in nums2 or i in nums3:\n",
    "                res.add(i)\n",
    "        for i in nums2:\n",
    "            if i in nums1 or i in nums3:\n",
    "                res.add(i)\n",
    "        return list(res)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        cnt = {}\n",
    "        ans = []\n",
    "        for n1 in nums1:\n",
    "            if n1 not in cnt:\n",
    "                cnt[n1] = 1\n",
    "        for n2 in set(nums2):\n",
    "            if n2 not in cnt:\n",
    "                cnt[n2] = 1\n",
    "            else:\n",
    "                cnt[n2] += 2\n",
    "        for n3 in set(nums3):\n",
    "            if n3 not in cnt:\n",
    "                cnt[n3] = 1\n",
    "            else:\n",
    "                cnt[n3] += 1\n",
    "        for k, v in cnt.items():\n",
    "            if v >= 2:\n",
    "                ans.append(k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        mask = defaultdict(int)\n",
    "        for i, nums in enumerate((nums1, nums2, nums3)):\n",
    "            for x in nums:\n",
    "                mask[x] |= 1 << i\n",
    "        return [x for x, m in mask.items() if m & (m - 1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        dict1 = {}\n",
    "        for i in set(nums1):\n",
    "            if i not in dict1.keys():\n",
    "                dict1[i] = 0\n",
    "\n",
    "        for i in set(nums2):\n",
    "            if dict1.get(i, None) == 0:\n",
    "                dict1[i] = 1\n",
    "            elif dict1.get(i) == 1:\n",
    "                continue\n",
    "            else:\n",
    "                dict1[i] = 0\n",
    "        for i in set(nums3):\n",
    "            if dict1.get(i, None) == 0:\n",
    "                dict1[i] = 1\n",
    "            elif dict1.get(i) == 1:\n",
    "                continue\n",
    "            else:\n",
    "                dict1[i] = 0\n",
    "        res = []\n",
    "        for k, v in dict1.items():\n",
    "            if v == 1:\n",
    "                res.append(k)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1 = set(nums1)\n",
    "        nums2 = set(nums2)\n",
    "        nums3 = set(nums3)\n",
    "        nums = nums1 | nums2 | nums3\n",
    "        count = {}\n",
    "        for i in nums:\n",
    "            if i in nums1:\n",
    "                count[i] = count.get(i,0)+1\n",
    "            if i in nums2:\n",
    "                count[i] = count.get(i,0)+1     \n",
    "            if i in nums3:\n",
    "                count[i] = count.get(i,0)+1       \n",
    "        return [num for num , cot in count.items() if cot >= 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        return list(set(nums1) & set(nums2) | set(nums1) & set(nums3) | set(nums2) & set(nums3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        res = set()\n",
    "        for i in nums1:\n",
    "            if i in nums2 or i in nums3:\n",
    "                res.add(i)\n",
    "        for i in nums2:\n",
    "            if i in nums1 or i in nums3:\n",
    "                res.add(i)        \n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums = [0]*101\n",
    "        for n in nums1,nums2,nums3:\n",
    "            for i in set(n):\n",
    "                nums[i] += 1\n",
    "        return [idx for idx,v in enumerate(nums) if v>1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1=set(nums1)\n",
    "        nums2=set(nums2)\n",
    "        nums3=set(nums3)\n",
    "        return list((nums1&nums2)|(nums2&nums3)|(nums3&nums1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        ls = set(nums1 + nums2 + nums3)\n",
    "        dic = {'nums1':set(nums1), 'nums2':set(nums2), 'nums3':set(nums3)}\n",
    "        res = []\n",
    "        for i in ls:\n",
    "            tmp = 0\n",
    "            for k in dic:\n",
    "                if i in dic[k]:\n",
    "                    tmp += 1\n",
    "            if tmp > 1:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        set1=list(set(nums1))\n",
    "        set2=list(set(nums2))\n",
    "        set3=list(set(nums3))\n",
    "        hash=[0]*101\n",
    "        res=[]\n",
    "        for i in range(len(set1)):\n",
    "            hash[set1[i]]+=1\n",
    "        for j in range(len(set2)):\n",
    "            hash[set2[j]]+=1\n",
    "        for k in range(len(set3)):\n",
    "            hash[set3[k]]+=1\n",
    "        for l in range(len(hash)):\n",
    "            if hash[l]>=2:\n",
    "                res.append(l)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        cnts = {}\n",
    "        \n",
    "        for i in nums1:\n",
    "            cnts.setdefault(i, [])\n",
    "            cnts[i].append(1) \n",
    "\n",
    "        for i in nums2:\n",
    "            cnts.setdefault(i, [])\n",
    "            cnts[i].append(2)\n",
    "\n",
    "        for i in nums3:\n",
    "            cnts.setdefault(i, [])\n",
    "            cnts[i].append(3)\n",
    "\n",
    "        for k, v in cnts.items():\n",
    "            if len(set(v)) >= 2:\n",
    "                res.append(k)\n",
    "        return res\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        return [k for k, v in Counter(list(set(nums1)) + list(set(nums2)) + list(set(nums3))).items() if v >= 2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        ls = set(nums1 + nums2 + nums3)\n",
    "        dic = {'nums1':set(nums1), 'nums2':set(nums2), 'nums3':set(nums3)}\n",
    "        res = []\n",
    "        for i in ls:\n",
    "            tmp = 0\n",
    "            for k in dic:\n",
    "                if i in dic[k]:\n",
    "                    tmp += 1\n",
    "            if tmp > 1:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        n=list(set(nums1))+list(set(nums2))+list(set(nums3))\n",
    "        n=collections.Counter(n)\n",
    "        x=[]\n",
    "        for i in n.keys():\n",
    "            if n[i]>=2:\n",
    "                x.append(i)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        set1, set2, set3 = set(nums1), set(nums2), set(nums3)\n",
    "        result = [i for i in range(1, 101) if (i in set1) + (i in set2) + (i in set3) > 1]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        s1 = list(set(nums1))\n",
    "        s2 = list(set(nums2))\n",
    "        s3 = list(set(nums3))\n",
    "        s = s1 + s2 + s3\n",
    "        dic = Counter(s)\n",
    "        ans = []\n",
    "        for k, v in dic.items():\n",
    "            if v >= 2:\n",
    "                ans.append(k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        s1, s2, s3 = set(nums1), set(nums2), set(nums3)\n",
    "        return [i for i in range(1, 101) if (i in s1) + (i in s2) + (i in s3) > 1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        from collections import Counter\n",
    "        nums1,nums2,nums3=list(set(nums1)),list(set(nums2)),list(set(nums3))\n",
    "        nums=nums1+nums2+nums3\n",
    "        zd=Counter(nums)\n",
    "        list_=[]\n",
    "        for key,vorde in zd.items():\n",
    "            if vorde>1:\n",
    "                list_.append(key)\n",
    "        return list_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        return list((set(nums1) & set(nums2)) | (set(nums1) & set(nums3)) | (set(nums2) & set(nums3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        set1 = set(nums1)  \n",
    "        set2 = set(nums2)  \n",
    "        set3 = set(nums3)  \n",
    "  \n",
    "        # 找出至少在两个集合中出现的元素  \n",
    "        common_elements = set1.intersection(set2) | set1.intersection(set3) | set2.intersection(set3)  \n",
    "  \n",
    "        # 将结果转换为列表并返回  \n",
    "        return list(common_elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1,nums2,nums3 = set(nums1),set(nums2),set(nums3)\n",
    "        return list(nums1 & nums2 | nums2 & nums3 | nums1 & nums3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        mask = defaultdict(int)\n",
    "        for i, nums in enumerate((nums1, nums2, nums3)):\n",
    "            for x in nums:\n",
    "                mask[x] |= 1 << i\n",
    "        return [x for x, m in mask.items() if m & (m - 1)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        from collections import defaultdict as ddict\n",
    "        map = ddict(int)\n",
    "        for s in [set(nums1), set(nums2), set(nums3)]:\n",
    "            for k in s:\n",
    "                map[k] += 1\n",
    "        l = []\n",
    "        for (k,v) in map.items():\n",
    "            if v >= 2:\n",
    "                l.append(k)\n",
    "        return l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        s1=set(nums1)\n",
    "        s2=set(nums2)\n",
    "        s3=set(nums3)\n",
    "        s12=s1&s2\n",
    "        s13=s1&s3\n",
    "        s23=s2&s3\n",
    "        return list(s12|s13|s23)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        return list((set(nums1)&set(nums2))|(set(nums1)&set(nums3))|(set(nums2)&set(nums3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        dict1 = {}\n",
    "        for i in set(nums1):\n",
    "            if i not in dict1.keys():\n",
    "                dict1[i] = 0\n",
    "\n",
    "        for i in set(nums2):\n",
    "            if dict1.get(i, None) == 0:\n",
    "                dict1[i] = 1\n",
    "            elif dict1.get(i) == 1:\n",
    "                continue\n",
    "            else:\n",
    "                dict1[i] = 0\n",
    "        for i in set(nums3):\n",
    "            if dict1.get(i, None) == 0:\n",
    "                dict1[i] = 1\n",
    "            elif dict1.get(i) == 1:\n",
    "                continue\n",
    "            else:\n",
    "                dict1[i] = 0\n",
    "        res = []\n",
    "        for k, v in dict1.items():\n",
    "            if v == 1:\n",
    "                res.append(k)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        mask = defaultdict(int)\n",
    "        for i, nums in enumerate((nums1, nums2, nums3)):\n",
    "            for x in nums:\n",
    "                mask[x] |= 1 << i\n",
    "        return [x for x, m in mask.items() if m & (m - 1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        seen = set(nums1)\n",
    "        ans = []\n",
    "        for x in set(nums2):\n",
    "            if x in seen:\n",
    "                ans.append(x)\n",
    "                seen.remove(x)\n",
    "            else:\n",
    "                seen.add(x)\n",
    "        for x in set(nums3):\n",
    "            if x in seen:\n",
    "                ans.append(x)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        lst=[]\n",
    "        for i in range(1,101):\n",
    "            if (i in nums1) + (i in nums2) + (i in nums3) >= 2:\n",
    "                lst.append(i)\n",
    "        return lst\n",
    "        # for i in nums1:\n",
    "        #     if i in nums2 or i in nums3:\n",
    "        #         lst.append(i)\n",
    "        # for i in nums2:\n",
    "        #     if i in nums1 or i in nums3:\n",
    "        #         lst.append(i)\n",
    "        # for i in nums3:\n",
    "        #     if i in nums1 or i in nums2:\n",
    "        #         lst.append(i)\n",
    "        # l1 = set(lst)\n",
    "        # l2 = list(l1)\n",
    "        # return l2\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        num=nums1+nums2+nums3\n",
    "        result=[]\n",
    "        for i in num:\n",
    "            if int(i in nums1) + int(i in nums2)+ int(i in nums3)>=2:\n",
    "                result.append(i)\n",
    "        return [i for i in set(result)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        ans = set()\n",
    "        ans |= set(nums1) & set(nums2)  # ans 与 nums1 与 nums2 的交集 合并后存回 ans 中\n",
    "        ans |= set(nums2) & set(nums3)\n",
    "        ans |= set(nums3) & set(nums1)\n",
    "        return list(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        ans = set()\n",
    "        dic = Counter(set(nums1))\n",
    "        for s in [set(nums2), set(nums3)]:\n",
    "            for n in s:\n",
    "                if n not in dic:\n",
    "                    dic[n] = 1\n",
    "                else:\n",
    "                    ans.add(n)\n",
    "        return list(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        nums1=list(set(nums1))\n",
    "        nums2=list(set(nums2))\n",
    "        nums3=list(set(nums3))\n",
    "        nums=Counter(nums1+nums2+nums3)\n",
    "        a=[]\n",
    "        for key,value in nums.items():\n",
    "            if value>=2:\n",
    "                a.append(key)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        s1 = set(nums1)\n",
    "        s2 = set(nums2)\n",
    "        s3 = set(nums3)\n",
    "        dic = Counter(s1)\n",
    "        for n in s2:\n",
    "            if n not in dic:\n",
    "                dic[n] = 1\n",
    "            else:\n",
    "                dic[n] += 1\n",
    "        for n in s3:\n",
    "            if n not in dic:\n",
    "                dic[n] = 1\n",
    "            else:\n",
    "                dic[n] += 1\n",
    "        ans = [k for k, v in dic.items() if v >= 2]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:\n",
    "        rec = {}\n",
    "        ans = set()\n",
    "        for index, nums in enumerate([nums1, nums2, nums3]):\n",
    "            for num in nums:\n",
    "                if num in rec:\n",
    "                    rec[num].add(index)\n",
    "                    if len(rec[num]) >= 2:\n",
    "                        ans.add(num)\n",
    "                else:\n",
    "                    rec[num] = {index}\n",
    "        print(list(ans))\n",
    "        return list(ans)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
